home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / dnsconf / misc.c < prev    next >
C/C++ Source or Header  |  1996-06-30  |  4KB  |  212 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include "dnsconf.h"
  6. #include "internal.h"
  7.  
  8. PUBLIC CACHEFILE::CACHEFILE(const char *_domain, const char *_path)
  9. {
  10.     domain.setfrom(_domain);
  11.     path.setfrom (_path);
  12. }
  13.  
  14. PUBLIC CACHEFILE* CACHEFILES::getitem(int no)
  15. {
  16.     return (CACHEFILE*)ARRAY::getitem(no);
  17. }
  18.  
  19.  
  20. PUBLIC IP_ADDR::IP_ADDR()
  21. {
  22.     a[0] = a[1] = a[2] = a[3] = -1;
  23. }
  24. PUBLIC IP_ADDR::IP_ADDR(const SSTRING &str)
  25. {
  26.     setfrom (str.get());
  27. }
  28. PUBLIC IP_ADDR::IP_ADDR(const IP_ADDR &adr)
  29. {
  30.     memcpy (a,adr.a,sizeof(a));
  31.     SSTRING::setfrom (adr.get());
  32. }
  33.  
  34. PUBLIC void IP_ADDR::setfrom (const char *pt)
  35. {
  36.     SSTRING::setfrom (pt);
  37.     int i=0;
  38.     a[0] = a[1] = a[2] = a[3] = -1;
  39.     while (isdigit(*pt) && i < 4){
  40.         a[i] = atoi(pt);
  41.         i++;
  42.         pt = ::strchr(pt,'.');
  43.         if (pt == NULL) break;
  44.         pt++;
  45.     }
  46. }
  47.  
  48. /*
  49.     Return != 0 if this is a valid IP number
  50. */
  51. PUBLIC int IP_ADDR::is_valid()
  52. {
  53.     int ret = 1;
  54.     for (int i=0; i<4; i++){
  55.         if (a[i] < 0 || a[i] > 255){
  56.             ret = 0;
  57.             break;
  58.         }
  59.     }
  60.     return ret;
  61. }
  62.  
  63. /*
  64.     Reformat the string representation from the four numbers
  65. */
  66. PUBLIC void IP_ADDR::reformat()
  67. {
  68.     char buf[20];
  69.     char *ctl = "%d";
  70.     char *pt = buf;
  71.     for (int i=0; i<4 && a[i] != -1; i++){
  72.         pt += sprintf (pt,ctl,a[i]);
  73.         ctl = ".%d";
  74.     }
  75.     SSTRING::setfrom (buf);
  76. }
  77. #if 0
  78. /*
  79.     Compute the domain for reverse mapping IN-ADDR.ARPA
  80. */
  81. PUBLIC void IP_ADDR::setrev (
  82.     int nbn,    // Number of IP number used to define to reverse
  83.             // domain
  84.     char *str)
  85. {
  86.     for (int i=nbn-1; i>=0; i--){
  87.         str += sprintf (str,"%u.",a[i]);
  88.     }
  89.     strcpy (str,"IN-ADDR.ARPA");
  90. }
  91. #endif
  92. /*
  93.     Compute the domain for reverse mapping IN-ADDR.ARPA
  94.     Guess the type of network from the amount ot -1's in the
  95.     address.
  96. */
  97. PUBLIC void IP_ADDR::setrev (
  98.     char *str)
  99. {
  100.     for (int i=3; i>=0; i--){
  101.         if (a[i] != -1){
  102.             str += sprintf (str,"%d.",a[i]);
  103.         }
  104.     }
  105.     strcpy (str,"IN-ADDR.ARPA");
  106. }
  107.  
  108. /*
  109.     Turn the IP number upside-down
  110. */
  111. PUBLIC void IP_ADDR::reverse()
  112. {
  113.     int b[4];
  114.     b[0] = a[3];
  115.     b[1] = a[2];
  116.     b[2] = a[1];
  117.     b[3] = a[0];
  118.     memcpy (a,b,sizeof(a));
  119.     reformat();
  120. }
  121.  
  122. PUBLIC int IP_ADDR::cmp(const IP_ADDR *p)
  123. {
  124.     int ret = 0;
  125.     for (int i=0; i<4; i++){
  126.         ret = a[i] - p->a[i];
  127.         if (ret != 0) break;
  128.     }
  129.     return ret;
  130. }
  131. /*
  132.     Shift the IP numbers to the left over the -1's (0.0.1.2 -> 1.2.0.0)
  133. */
  134. PUBLIC void IP_ADDR::shift()
  135. {
  136.     for (int i=0; i<4; i++){
  137.         if (a[0] == -1){
  138.             memmove (a,a+1,3*sizeof(a[0]));
  139.             a[3] = -1;
  140.         }else{
  141.             break;
  142.         }
  143.     }
  144.     reformat();
  145. }
  146. /*
  147.     Shift the IP numbers to the right over the -1's (1.2.0.0 -> 0.0.1.2)
  148. */
  149. PUBLIC void IP_ADDR::shift_right()
  150. {
  151.     for (int i=3; i>0; i--){
  152.         if (a[3] == -1){
  153.             memmove (a+1,a,3*sizeof(a[0]));
  154.             a[0] = -1;
  155.         }else{
  156.             break;
  157.         }
  158.     }
  159.     reformat();
  160. }
  161.  
  162. /*
  163.     Augment the IP number by one.
  164.     No reformat is done since this function is called often
  165. */
  166. PUBLIC void IP_ADDR::increm()
  167. {
  168.     for (int i=3; i>=0; i--){
  169.         a[i]++;
  170.         if (a[i] < 256) break;
  171.         a[i] = 0;
  172.     }
  173. }
  174. /*
  175.     Merge one IP number end over another.
  176. */
  177. PUBLIC void IP_ADDR::merge(IP_ADDR &partial)
  178. {
  179.     for (int i=0; i<4; i++){
  180.         if (partial.a[i] != -1) a[i] = partial.a[i];
  181.     }
  182.     reformat();
  183. }
  184. /*
  185.     Make sure there is a trailing dot at the end of the string
  186. */
  187. void dns_cnv2abs(SSTRING &s)
  188. {
  189.     const char *str = s.get();
  190.     int len = strlen(str);
  191.     if (len == 0 || str[len-1] != '.') s.append(".");
  192. }
  193.  
  194.  
  195. PUBLIC IP_ADDR *IP_ADDRS::getitem(int no) const
  196. {
  197.     return (IP_ADDR*)ARRAY::getitem(no);
  198. }
  199.  
  200. static int cmp_by_addr (const ARRAY_OBJ *op1, const ARRAY_OBJ *op2)
  201. {
  202.     IP_ADDR *p1 = (IP_ADDR*)op1;
  203.     IP_ADDR *p2 = (IP_ADDR*)op2;
  204.     return p1->cmp(p2);
  205. }
  206.  
  207. PUBLIC void IP_ADDRS::sort()
  208. {
  209.     ARRAY::sort(cmp_by_addr);
  210. }
  211.  
  212.